package se.hrbustrc.hlgshopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import se.hrbustrc.hlgshopping.entity.PageResult;
import se.hrbustrc.hlgshopping.entity.Product;
import se.hrbustrc.hlgshopping.service.ProductService;
import se.hrbustrc.hlgshopping.utils.Result;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/list")
    public Result listProduct(){
        try {
            Result result = new Result();
            result.ok(productService.list());
            return result;
        } catch (Exception e) {
            return new Result().error("获取商品列表失败: " + e.getMessage());
        }
    }

    // 根据ID获取商品详情
    @GetMapping("/{id}")
    public Result getProductDetail(@PathVariable Integer id) {
        if (id == null) {
            return new Result().error("商品ID不能为空");
        }

        try {
            // 假设ProductService中有根据ID查询的方法
            Object product = productService.getById(id);
            if (product == null) {
                return new Result().error("未找到指定商品");
            }
            return new Result().ok(product);
        } catch (Exception e) {
            return new Result().error("获取商品详情失败: " + e.getMessage());
        }
    }
    // 分页查询
    @RequestMapping("/page")
    public PageResult<Product> getProductPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String pName) {

        Page<Product> page = new Page<>(current, size);
        LambdaQueryWrapper<Product> query = Wrappers.<Product>lambdaQuery();

        if (StringUtils.isNotBlank(pName)) {
            query.like(Product::getPname, pName);
        }

        Page<Product> productPage = productService.page(page, query);
        return PageResult.convert(productPage);
    }
    /**
     * 增加商品
     */
    @RequestMapping("/add")
    public Result addProduct(Product product) {
        productService.addProduct(product);
        return new Result().ok("添加成功");

    }
    /**
     * 修改商品
     */
    @RequestMapping("/edit")
    public Result editProduct(Product product) {
        productService.editProduct(product);
        return new Result().ok("修改成功");
    }
    /**
     * 删除商品
     */
    @RequestMapping("/delete/{id}")
    public Result deleteProduct(@PathVariable("id") int id) {
        productService.delProduct(id);
        return new Result().ok("删除成功");
    }
    /**
     * 商品上架
     */
    @RequestMapping("/upstate/{id}")
    public Result upStateProduct(@PathVariable("id") int id) {
        String sql=String.format("update tb_product set FPListSate=1 where fpid=%s",id);
        return new Result().ok("上架成功");
    }
    /**
     * 商品批量上架
     */
    public Result upStateBatchProduct(String[]  id) {
        String sql=String.format("update tb_product set FPListSate=1 where fpid in(%s)",id);
        return new Result().ok("上架成功");
    }
    /**
     * 商品下架
     */
    @RequestMapping("/downstate/{id}")
    public Result downStateProduct(@PathVariable("id") int id) {
        String sql=String.format("update tb_product set FPListSate=0 where fpid=%s",id);
        return new Result().ok("下架成功");
    }
    /**
     * 批量下架
     */
    @RequestMapping("/downstate")
    public Result downStateBatchProduct(String[]  id) {
        String sql=String.format("update tb_product set FPListSate=0 where fpid in(%s)",id);
        return new Result().ok("下架成功");
    }
}
